/********************************
* name:	通信管理模块
* func:	串口指令解析
********************************/
#include "command_handler.h"
system_uart_cmd_state user_cmd_t = RUN_STATE_IDLE;//系统指令枚举 默认为空指令
uint8_t cmd_run_flag;//0 - 延迟执行 1 - 立即执行
uint8_t uart_read_buffer[BUFFER_SIZE];//dma空闲中断方案，数据待处理缓冲区
Config_t user_ctr = {1.0,100};//系统参数配置值
/**
 * @brief 串口任务函数
 * @param none
 * @return none
 */
void uart_task(void)
{

    //ringbuffer+dma+空闲中断
    uint16_t pool_length;//有效数据长度
	//获取当前数据长度
    pool_length = rt_ringbuffer_data_len(&uart_ringbuffer_t);
    //若无有效数据则直接返回
	if(pool_length == 0) return;

	//获取待解析数据
    rt_ringbuffer_get(&uart_ringbuffer_t,uart_read_buffer,pool_length);
	/************************指令处理**********************/
	uart_read_buffer[pool_length] = '\0';
	//系统自检
	if(strcmp((const char *)uart_read_buffer,"test") == 0)
	{
		//系统自检指令
		user_cmd_t = RUN_STATE_CHECK;
		//写入日志
		log_operation("system harware test", " ");
		cmd_run_flag = 1;//立即执行
	}
	//时间设置
	else if(strcmp((const char *)uart_read_buffer,"RTC Config") == 0)
	{
		user_printf(&huart3,"Input DateTime\r\n");
		user_cmd_t = RUN_STATE_RTC_SET;
		//日志
		log_operation("rtc config", " ");
		cmd_run_flag = 0;//延迟执行
	}
	//读取时间
	else if(strcmp((const char *)uart_read_buffer,"RTC now") == 0)
	{
		user_cmd_t = RUN_STATE_RTC_GET;
		cmd_run_flag = 1;//立即执行
	}
	//读取配置
	else if(strcmp((const char *)uart_read_buffer,"conf") == 0)
	{
		user_cmd_t = RUN_STATE_CONF_READ;
		cmd_run_flag = 1;//立即执行
	}
	//变比设置
	else if(strcmp((const char *)uart_read_buffer,"ratio") == 0)
	{
		user_cmd_t = RUN_STATE_CONF_SET_R;
		cmd_run_flag = 0;//延迟执行
		Config_t temp_config = read_config_ini(0);
		user_printf(&huart3,"Ratio=%.2f\r\n", temp_config.ratio);
		user_printf(&huart3,"Input vaule(0-100):\r\n");
			
		log_operation("ratio config"," ");
	}
	//阈值设置
	else if(strcmp((const char *)uart_read_buffer,"limit") == 0)
	{
		user_cmd_t = RUN_STATE_CONF_SET_L;
		cmd_run_flag = 0;//延迟执行
		Config_t temp_config = read_config_ini(0);
		user_printf(&huart3,"Limit=%.2f\r\n", temp_config.limit);
		user_printf(&huart3,"Input vaule(0-500):\r\n");
			
		log_operation("limit config"," ");
	}
	//参数存储
	else if(strcmp((const char *)uart_read_buffer,"config save") == 0)
	{
		user_cmd_t = RUN_STATE_CONF_SAVE;
		cmd_run_flag = 1;//立即执行
	}
	//读取FLASH参数
	else if(strcmp((const char *)uart_read_buffer,"config read") == 0)
	{
		user_cmd_t = RUN_STATE_CONF_FLASH_R;
		cmd_run_flag = 1;//立即执行
	}
	//采样启动
	else if(strcmp((const char*)uart_read_buffer,"start") == 0)
	{
		user_cmd_t = RUN_STATE_SAMPLE_START;
		cmd_run_flag = 1;//立即执行
	}
	//采样关闭
	else if(strcmp((const char *)uart_read_buffer,"stop") == 0)
	{
		user_cmd_t = RUN_STATE_SAMPLE_STOP;
		cmd_run_flag = 1;//立即执行
	}
	//数据加密
	else if(strcmp((const char *)uart_read_buffer,"hide") == 0)
	{
		user_cmd_t = RUN_STATE_DAT_HIDE;
		cmd_run_flag = 1;//立即执行
	}
	//数据解加密
	else if(strcmp((const char *)uart_read_buffer,"unhide") == 0)
	{
		user_cmd_t = RUN_STATE_DAT_UNHIDE;
		cmd_run_flag = 1;//立即执行
	}
	//串口指令执行
	if(user_cmd_t != RUN_STATE_IDLE)
	{
		if(cmd_run_flag == 1)//立即执行
		{
			system_uart_cmd_run((char *)uart_read_buffer);
		}
		else
		{
			cmd_run_flag = 1;//更新标志位,等待下一轮数据执行
		}
	}
	/************************指令处理**********************/
	memset(uart_read_buffer,0,sizeof(uart_read_buffer));
}

/**
 * @brief 系统指令运行函数
 * @param none
 * @return none
 */
void system_uart_cmd_run(char *buffer)
{
	//系统自检
	if(user_cmd_t == RUN_STATE_CHECK)
	{
		system_self_test();
		user_cmd_t = RUN_STATE_IDLE;
	}
	//时间设置
	else if(user_cmd_t == RUN_STATE_RTC_SET)
	{
		HAL_StatusTypeDef status = rtc_set_time_from_string(buffer);
		if (status == HAL_OK)
		{
			user_printf(&huart3, "RTC Config success\r\n");
			user_printf(&huart3, "Time: %s\r\n", buffer);
			//storage_log_operation("RTC_CONFIG", buffer);
			log_operation("rtc config success to ",buffer);
			user_cmd_t = RUN_STATE_IDLE;
		}
		else
		{
			user_printf(&huart3, "RTC Config failed\r\n");
			user_printf(&huart3, "Invalid time format. Please use: 2025-01-01 15:00:10 or 2025年01月01日12:00:30\r\n");
		}
	}
	//时间获取
	else if(user_cmd_t == RUN_STATE_RTC_GET)
	{
		user_printf(&huart3,"Current Time:");
		rtc_print_current_time();
		user_printf(&huart3,"\r\n");
		user_cmd_t = RUN_STATE_IDLE;
	}
	//配置读取
	else if(user_cmd_t == RUN_STATE_CONF_READ)
	{
		Config_t temp_config = read_config_ini(2);
		user_printf(&huart3,"Ratio=%.2f\r\n", temp_config.ratio);
    	user_printf(&huart3,"Limit=%.2f\r\n", temp_config.limit);
    	user_printf(&huart3,"config read success\r\n");
		user_cmd_t = RUN_STATE_IDLE;
	}
	//变比设置
	else if(user_cmd_t == RUN_STATE_CONF_SET_R)
	{
		int result = 0;
		int num = 0;
		float temp_r;
		result = sscanf(buffer,"%f%n",&temp_r,&num);
		if(result == 1 && strlen(buffer) == num)
		{
			if(temp_r >= 0 && temp_r <= 100)
			{
				update_config_value("Ratio",temp_r);

				user_printf(&huart3,"ratio modified success\r\n");
				Config_t temp_config = read_config_ini(0);
				user_printf(&huart3,"Ratio=%.2f\r\n", temp_config.ratio);
				user_ctr.ratio = temp_config.ratio;
				//日志
				char log_msg[128];
				sprintf(log_msg, "success to %.2f", temp_r);
				log_operation("ratio config ", log_msg);
			}
			else//参数无效
			{
				user_printf(&huart3,"ratio invalid\r\n");
				Config_t temp_config = read_config_ini(0);
				user_printf(&huart3,"Ratio=%.2f\r\n", temp_config.ratio);
			}
		}
		else//参数无效
		{
			user_printf(&huart3,"ratio invalid\r\n");
			Config_t temp_config = read_config_ini(0);
			user_printf(&huart3,"Ratio=%.2f\r\n", temp_config.ratio);
		}
		user_cmd_t = RUN_STATE_IDLE;
	}
	//阈值设置
	else if(user_cmd_t == RUN_STATE_CONF_SET_L)
	{
		int result = 0;
		int num = 0;
		float temp_l;
		result = sscanf(buffer,"%f%n",&temp_l,&num);
		if(result == 1 && strlen(buffer) == num)
		{
			if(temp_l >= 0 && temp_l <= 500)
			{
				update_config_value("Limit",temp_l);
				user_printf(&huart3,"limit modified success\r\n");
				Config_t temp_config = read_config_ini(0);
				user_printf(&huart3,"Limit=%.2f\r\n", temp_config.limit);

				user_ctr.limit = temp_config.limit;
						
					
				//日志
				char log_msg_1[128];
				sprintf(log_msg_1, "success to %.2f", temp_l);
				log_operation("limit config ", log_msg_1);
			}
			else//参数无效
			{
				user_printf(&huart3,"limit invalid\r\n");
				Config_t temp_config = read_config_ini(0);
				user_printf(&huart3,"Limit=%.2f\r\n", temp_config.limit);
			}
		}
		else//参数无效
		{
			user_printf(&huart3,"limit invalid\r\n");
			Config_t temp_config = read_config_ini(0);
			user_printf(&huart3,"Limit=%.2f\r\n", temp_config.limit);
		}
		user_cmd_t = RUN_STATE_IDLE;
	}
	//参数存储
	else if(user_cmd_t == RUN_STATE_CONF_SAVE)
	{
		Config_t temp_config = read_config_ini(1);
		user_printf(&huart3,"ratio:%.2f\r\nLlimit=%.2f\r\n", temp_config.ratio,temp_config.limit);
		user_printf(&huart3,"save parameters to flash\r\n");
		user_cmd_t = RUN_STATE_IDLE;
	}
	//flash读取参数
	else if(user_cmd_t == RUN_STATE_CONF_FLASH_R)
	{
		char flash_re_dat[256];
		
		memset(flash_re_dat, 0, SPI_FLASH_PAGE_SIZE);
		spi_flash_buffer_read(flash_re_dat, 0, SPI_FLASH_PAGE_SIZE);
		sscanf(flash_re_dat,"[Ratio]=%f,[Limit]=%f",&user_ctr.ratio,&user_ctr.limit);
		//user_printf(&huart3, "Data read:%s\r\n", flash_re_dat);
		
		user_printf(&huart3,"read parameters from flash\r\n");
		user_printf(&huart3,"ratio:%.2f\r\nlimit:%.2f\r\n",user_ctr.ratio,user_ctr.limit);
		//user_printf(&huart3, "Data read:%s\r\n", flash_re_dat);
		user_cmd_t = RUN_STATE_IDLE;
	}
	//采样启动
	else if(user_cmd_t == RUN_STATE_SAMPLE_START)
	{
		user_printf(&huart3,"Periodic Sampling\r\n");
		user_printf(&huart3,"sample cycle:%ds\r\n",adc_sample_time);
		//启动采样
		adc_run_state = 1;
		OLED_Clear();
		//adc_out_time = uwTick;
		//日志
		char log_msg_start[128];
		sprintf(log_msg_start, "%ds (command)", adc_sample_time);
		log_operation("sample start - cycle ", log_msg_start);
		user_cmd_t = RUN_STATE_IDLE;
	}
	//采样关闭
	else if(user_cmd_t == RUN_STATE_SAMPLE_STOP)
	{
		user_printf(&huart3,"Periodic Sampling STOP\r\n");
		adc_run_state = 0;
		storage_system_close();
		//日志
		log_operation("sample stop", "periodic sampling stopped");
		user_cmd_t = RUN_STATE_IDLE;
	}
	//数据加密
	else if(user_cmd_t == RUN_STATE_DAT_HIDE)
	{
		output_time_type = 1;//数据加密模式
		log_operation("hide data", " ");
		user_cmd_t = RUN_STATE_IDLE;
	}
	//数据解加密
	else if(user_cmd_t == RUN_STATE_DAT_UNHIDE)
	{
		output_time_type = 0;//数据解加密
		log_operation("unhide data", " ");
		user_cmd_t = RUN_STATE_IDLE;
	}
}